<template>
  <view :class="['page-app', theme]">
    <view class="container">
      <router-view></router-view>
    </view>
  </view>
</template>

<script setup>
  import { onLaunch, onShow, onHide, onUnload, onError } from '@dcloudio/uni-app';
  import { ShoproInit } from './sheep';
  import { useWebSocket } from '@/sheep/hooks/useWebSocket';
  import { WebSocketMessageTypeConstants } from '@/sheep/util/const';
  import { computed } from 'vue';
  import sheep from '@/sheep';

  // 使用计算属性获取主题
  const theme = computed(() => sheep.$store('sys').theme);

  onLaunch(() => {
    // 隐藏原生导航栏 使用自定义底部导航
    //uni.hideTabBar();
	
    // 加载Shopro底层依赖
    ShoproInit();

    // 检查登录状态并跳转
    checkLoginAndRedirect();
  });

  // 检查登录状态并跳转到对应页面
  function checkLoginAndRedirect() {
    const token = uni.getStorageSync('token');
     
    // 如果已登录且在登录页，跳转到首页
    if (token) {
      uni.reLaunch({
        url: '/pages/tabbar/index'
      });
      return;
    }

    // 如果未登录且不在登录页，跳转到登录页
    if (!token) {
      setTimeout(() => {
        uni.reLaunch({
          url: '/pages/index/login'
        });
      }, 100);
      return;
    }
  }

  onError((err) => {
    console.log('AppOnError:', err);
  });

  onShow((options) => {
    // #ifdef APP-PLUS
    // 获取urlSchemes参数
    const args = plus.runtime.arguments;
    if (args) {
    } 

    // 获取剪贴板
    uni.getClipboardData({
      success: (res) => { },
    });
    // #endif

    // #ifdef MP-WEIXIN
    // 确认收货回调结果
    console.log(options,'options');
    // #endif
	
	// 禁止截屏
	if (wx.setVisualEffectOnCapture) {
		wx.setVisualEffectOnCapture({
			visualEffect: 'hidden',
			complete: function(res) {}
		})
	}
	
	if (wx.setVisualEffectOnCapture) {
		let that = this
		wx.onUserCaptureScreen(function (res) {
			uni.showToast({
				icon: 'none',
				title: '禁止截屏！',
				position: 'bottom'
			})
			that.flushed()
		})
	}
	console.log('App Show')
  });
  
  onHide(() => {
	  if (wx.setVisualEffectOnCapture) {
	  	wx.setVisualEffectOnCapture({
	  		visualEffect: 'none',
	  		complete: function(res) {}
	  	})
	  }
  });
  
  onUnload(() => {
  	if (wx.setVisualEffectOnCapture) {
  		wx.setVisualEffectOnCapture({
  			visualEffect: 'none',
  			complete: function(res) {}
  		})
  	}
  });
  
  //======================= 聊天工具相关 end =======================
  /* const { options } = useWebSocket({
    // 连接成功
    onConnected: async () => {
		console.log('连接成功');
    },
    // 收到消息
    onMessage: async (data) => {
		console.log('接收消息');
		const type = data.type;
		if (!type) {
			console.error('未知的消息类型：' + data.value);
			return;
		}
		if (type == WebSocketMessageTypeConstants.IM_MESSAGE_READ) {
			console.log('刷新消息');
			setTimeout(function(){
				uni.$emit(WebSocketMessageTypeConstants.IM_MESSAGE_READ, data);
			},1000);
			return;
		}
		
		if (type == WebSocketMessageTypeConstants.IM_MESSAGE_NEWS) {
			uni.$emit(WebSocketMessageTypeConstants.IM_MESSAGE_NEWS, data);
			return;
		}
		
		// 2.2 消息类型：KEFU_MESSAGE_TYPE
		if (type == WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
			console.log('刷新消息');
			// 刷新消息列表
			//await messageListRef.value.refreshMessageList(JSON.parse(data.content));
			return;
		}
		// 2.3 消息类型：KEFU_MESSAGE_ADMIN_READ
		if (type == WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
			console.log('管理员已读消息');
		}
    },
  }); */
</script>

<style lang="scss">
	@import '@/sheep/scss/index.scss'; 
	@import "@/static/css/iconfont.css";
	
	::-webkit-scrollbar {
		width: 0 !important;
	  	height: 0 !important;
	  	color: transparent !important;
	 	display: none;
	}	
	
  // 声明字体
  @font-face {
    font-family: 'fangyuan';
	src: url('~@/static/AlimamaFangYuanTiVF-Thin.ttf') format('truetype');
    /* 如果有其他格式的字体文件，可以添加多个 src */
    /* src: url('/static/fonts/fangyuan.woff2') format('woff2'),
           url('/static/fonts/fangyuan.woff') format('woff'),
           url('/static/fonts/fangyuan.ttf') format('truetype'); */
    font-weight: normal;
    font-style: normal;
    font-display: swap;  // 优化字体加载
  }

  // 添加深色主题变量
  :root {
	//主要颜色
    --bg-main: #121212;
	//卡片颜色
    --bg-card: #1e1e1e;
	//卡片次要颜色
	--bg-card-secondary: #757575;
	//按钮颜色
    --button-main: #FCD99F;
	//文字主要颜色
    --text-main: #E0E0E0;
	//文字次要颜色
    --text-secondary: rgba(255, 255, 255, 0.7);
	//边框颜色
    --border-color: linear-gradient(to right, #434343 0%, #1C1C1C 150%);
  }

  // 全局应用深色背景
  page {
    background-color: var(--bg-main) !important;
    color: var(--text-main);
    font-family: 'fangyuan', sans-serif !important;
  }

  // 确保所有 page-app class 都有深色背景
  .page-app {
    background-color: var(--bg-main) !important;
    min-height: 100vh;
  }

  // 卡片背景
  .card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
  }

  ::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
    color: transparent !important;
    display: none;
  }
</style>
